/*
 * bootSector.c
 *
 *  Created on: 19/11/2011
 *      Author: utn_so
 */

#include "bootSector.h"

extern t_pfs pPfs;

int32_t leerCargarBootSector(void){
	int offset=0;
	t_file_block *fileBlock;
//	file=fileio_openfile("/home/utn_so/Escritorio/fat32.disk4");
	t_sector *sector;


	sector  = pedirSector(0);

//	fileBlock=fileio_readfileblock("/home/utn_so/Escritorio/fat32.disk4",512,0);

//Pido el primer sector que es donde esta el bootsector


	printf("Struct Size: %d",sizeof(pPfs.fat32.bootSector));
	printf("antes memcpy\n");

//Copio la informacion a la estructura que manejamos de fat
	memcpy(pPfs.fat32.bootSector.jumpInstruction,sector->data+offset,3);
	offset=offset+sizeof(pPfs.fat32.bootSector.jumpInstruction);
	printf("Offset: %d \n",offset);

	printf("fat32.bootSector.jumpInstruction :%s \n",pPfs.fat32.bootSector.jumpInstruction);

	memcpy(pPfs.fat32.bootSector.OEMName,sector->data+offset,8);
	offset=offset+sizeof(pPfs.fat32.bootSector.OEMName);
	printf("pPfs.fat32.bootSector.OEMName :%s \n",pPfs.fat32.bootSector.OEMName);
	printf("Offset: %d \n",offset);

	memcpy(&(pPfs.fat32.bootSector.bytesPerSector),sector->data+offset,2);
	offset=offset+sizeof(pPfs.fat32.bootSector.bytesPerSector);
	printf("pPfs.fat32.bootSector.bytesPerSector :%u \n",pPfs.fat32.bootSector.bytesPerSector);
	printf("Offset: %d \n",offset);

	memcpy(&(pPfs.fat32.bootSector.sectorsPerCluster),sector->data+offset,1);
	offset=offset+sizeof(pPfs.fat32.bootSector.sectorsPerCluster);
	printf("pPfs.fat32.bootSector.sectorsPerCluster :%c \n",pPfs.fat32.bootSector.sectorsPerCluster);
	printf("Offset: %d \n",offset);

	memcpy(&(pPfs.fat32.bootSector.reservedSectorCount),sector->data+offset,2);
	offset=offset+sizeof(pPfs.fat32.bootSector.reservedSectorCount);
	printf("pPfs.fat32.bootSector.reservedSectorCount :%c \n",pPfs.fat32.bootSector.reservedSectorCount);
	printf("Offset: %d \n",offset);

	memcpy(&(pPfs.fat32.bootSector.numberOfFATS),sector->data+offset,1);
	offset=offset+sizeof(pPfs.fat32.bootSector.numberOfFATS);
	printf("pPfs.fat32.bootSector.numberOfFATS :%c \n",pPfs.fat32.bootSector.numberOfFATS);
	printf("Offset: %d \n",offset);


	memcpy(&pPfs.fat32.bootSector.numberOfRootDirectoriesEntries,sector->data+offset,2);
	offset=offset+sizeof(pPfs.fat32.bootSector.numberOfRootDirectoriesEntries);
	printf(" pPfs.fat32.bootSector.numberOfRootDirectoriesEntries:%u \n",pPfs.fat32.bootSector.numberOfRootDirectoriesEntries);
	printf("Offset: %d \n",offset);


	memcpy(&pPfs.fat32.bootSector.totalSectors,sector->data+offset,2);
	offset=offset+sizeof(pPfs.fat32.bootSector.totalSectors);
	printf(" pPfs.fat32.bootSector.totalSectors:%u \n",pPfs.fat32.bootSector.totalSectors);
	printf("Offset: %d \n",offset);

	memcpy(&pPfs.fat32.bootSector.mediaDescriptor,sector->data+offset,1);
	offset=offset+sizeof(pPfs.fat32.bootSector.mediaDescriptor);
	printf("pPfs.fat32.bootSector.mediaDescriptor :%c \n",pPfs.fat32.bootSector.mediaDescriptor);
	printf("Offset: %d \n",offset);


	memcpy(&pPfs.fat32.bootSector.sectorsPerFat,sector->data+offset,2);
	offset=offset+sizeof(pPfs.fat32.bootSector.sectorsPerFat);
	printf("pPfs.fat32.bootSector.sectorsPerFat :%u \n",pPfs.fat32.bootSector.sectorsPerFat);
	printf("Offset: %d \n",offset);


	memcpy(&pPfs.fat32.bootSector.sectorsPerTrack,sector->data+offset,2);
	offset=offset+sizeof(pPfs.fat32.bootSector.sectorsPerTrack);
	printf("pPfs.fat32.bootSector.sectorsPerTrack :%u \n",pPfs.fat32.bootSector.sectorsPerTrack);
	printf("Offset: %d \n",offset);


	memcpy(&pPfs.fat32.bootSector.numberOfHeads,sector->data+offset,2);
	offset=offset+sizeof(pPfs.fat32.bootSector.numberOfHeads);
	printf("Offset: %d \n",offset);
	printf("pPfs.fat32.bootSector.numberOfHeads :%u \n",pPfs.fat32.bootSector.numberOfHeads);



	memcpy(&pPfs.fat32.bootSector.hiddenSectorsBeforeFAT,sector->data+offset,4);
	offset=offset+sizeof(pPfs.fat32.bootSector.hiddenSectorsBeforeFAT);
	printf("Offset: %d \n",offset);
	printf("pPfs.fat32.bootSector.hiddenSectorsBeforeFAT :%d \n",pPfs.fat32.bootSector.hiddenSectorsBeforeFAT);


	memcpy(&pPfs.fat32.bootSector.totalSectorsExt,sector->data+offset,4);
	offset=offset+sizeof(pPfs.fat32.bootSector.totalSectorsExt);
	printf("pPfs.fat32.bootSector.totalSectorsExt :%d \n",pPfs.fat32.bootSector.totalSectorsExt);
	printf("Offset: %d \n",offset);


//	memcpy(&pPfs.fat32.extendBios.clusterRootDirectory,file+0x2C,4);
//	printf("clusterRoot %d  \n",pPfs.fat32.extendBios.clusterRootDirectory);

	memcpy(&pPfs.fat32.extendBios.sectorsFAT,sector->data+0x24,4);
	offset=offset+sizeof(pPfs.fat32.extendBios.sectorsFAT);
	printf("pPfs.fat32.extendBios.sectorsFAT :%d \n",pPfs.fat32.extendBios.sectorsFAT);
	printf("Offset: %d \n",offset);



	memcpy(&pPfs.fat32.extendBios.fatFlags,sector->data+0x28,2);
	offset=offset+sizeof(pPfs.fat32.extendBios.fatFlags);
	printf("pPfs.fat32.extendBios.fatFlags :%d \n",pPfs.fat32.extendBios.fatFlags);
	printf("Offset: %d \n",offset);

	memcpy(&pPfs.fat32.extendBios.version,sector->data+0x2A,2);
	offset=offset+sizeof(pPfs.fat32.extendBios.version);
	printf("pPfs.fat32.extendBios.version :%d \n",pPfs.fat32.extendBios.version);
	printf("Offset: %d \n",offset);

	memcpy(&pPfs.fat32.extendBios.clusterRootDirectory,sector->data+0x2C,4);
	offset=offset+sizeof(pPfs.fat32.extendBios.clusterRootDirectory);
	printf("pPfs.fat32.extendBios.clusterRootDirectory :%d \n",pPfs.fat32.extendBios.clusterRootDirectory);
	printf("Offset: %d \n",offset);

	memcpy(&pPfs.fat32.extendBios.fsInfoSector,sector->data+0x30,2);
	offset=offset+sizeof(pPfs.fat32.extendBios.fsInfoSector);
	printf("pPfs.fat32.extendBios.fsInfoSector :%d \n",pPfs.fat32.extendBios.fsInfoSector);
	printf("Offset: %d \n",offset);


	memcpy(&pPfs.fat32.extendBios.sectorCopyBootSector,sector->data+0x32,2);
	offset=offset+sizeof(pPfs.fat32.extendBios.sectorCopyBootSector);
	printf("pPfs.fat32.extendBios.sectorCopyBootSector :%d \n",pPfs.fat32.extendBios.sectorCopyBootSector);
	printf("Offset: %d \n",offset);


	memcpy(pPfs.fat32.extendBios.reserved,sector->data+0x34,12);
	offset=offset+sizeof(pPfs.fat32.extendBios.reserved);
	printf("pPfs.fat32.extendBios.reserved :%s \n",pPfs.fat32.extendBios.reserved);
	printf("Offset: %d \n",offset);


	memcpy(&pPfs.fat32.extendBios.physicalDriveNumber,sector->data+0x40,1);
	offset=offset+sizeof(pPfs.fat32.extendBios.physicalDriveNumber);
	printf("pPfs.fat32.extendBios.physicalDriveNumber :%c \n",pPfs.fat32.extendBios.physicalDriveNumber);
	printf("Offset: %d \n",offset);

	memcpy(&pPfs.fat32.extendBios.reserved2,sector->data+0x41,1);
	offset=offset+sizeof(pPfs.fat32.extendBios.reserved2);
	printf("pPfs.fat32.extendBios.reserved2 :%c \n",pPfs.fat32.extendBios.reserved2);
	printf("Offset: %d \n",offset);


	memcpy(&pPfs.fat32.extendBios.extendedbootSignature,sector->data+0x42,1);
	offset=offset+sizeof(pPfs.fat32.extendBios.extendedbootSignature);
	printf("pPfs.fat32.extendBios.extendedbootSignature :%c \n",pPfs.fat32.extendBios.extendedbootSignature);
	printf("Offset: %d \n",offset);


	memcpy(&pPfs.fat32.extendBios.idSerialNumber,sector->data+0x43,4);
	offset=offset+sizeof(pPfs.fat32.extendBios.idSerialNumber);
	printf("pPfs.fat32.extendBios.idSerialNumber :%d \n",pPfs.fat32.extendBios.idSerialNumber);
	printf("Offset: %d \n",offset);



	memcpy(pPfs.fat32.extendBios.volumeLabel,sector->data+0x47,12);
	offset=offset+sizeof(pPfs.fat32.extendBios.volumeLabel);
	printf("pPfs.fat32.extendBios.volumeLabel :%s \n",pPfs.fat32.extendBios.volumeLabel);
	printf("Offset: %d \n",offset);



	memcpy(pPfs.fat32.extendBios.fileSystemType,sector->data+0x52,12);
	offset=offset+sizeof(pPfs.fat32.extendBios.fileSystemType);
	printf("pPfs.fat32.extendBios.fileSystemType :%s \n",pPfs.fat32.extendBios.fileSystemType);
	printf("Offset: %d \n",offset);

//libero el sector pedido
	free(sector);
	pPfs.configArch.blockSize=2;

	return 1;
	}


t_sector * cargarBootSector(void){
return 	pedirBootSector();
}
